<template>
    <cl-crud :ref="setRefs('crud')" @load="onLoad">
        <el-row type="flex" align="middle">
            <!-- 刷新按钮 -->
            <cl-refresh-btn />
            <!-- 新增按钮 -->
            <cl-add-btn />
            <!-- 删除按钮 -->
            <cl-multi-delete-btn />
            <cl-flex1 />
            <!-- 关键字搜索 -->
            <cl-search-key />
        </el-row>

        <el-row>
            <!-- 数据表格 -->
            <cl-table :ref="setRefs('table')" v-bind="table" />
        </el-row>

        <el-row type="flex">
            <cl-flex1 />
            <!-- 分页控件 -->
            <cl-pagination />
        </el-row>

        <!-- 新增、编辑 -->
        <cl-upsert :ref="setRefs('upsert')" v-bind="upsert" />
    </cl-crud>
</template>

<script lang="ts">
import { defineComponent, reactive } from "vue";
import { CrudLoad, Upsert, Table } from "cl-admin-crud-vue3/types";
import { useCool } from "/@/core";

export default defineComponent({
    setup() {
        // 功能
        const { refs, setRefs, service } = useCool();

        // 新增、编辑配置
        const upsert = reactive<Upsert>({
            items: [
                {
					prop: "name",
					label: "标签名称",
					component: {
						name: "el-input",
						props: {
							placeholder: "请输入标签名称",
							autofocus: true
						}
					},
					rules: {
						required: true,
						message: "名称不能为空"
					}
				},
                {
					prop: "sortNum",
					label: "排序等级",
					value: 0,
					component: {
						name: "el-input-number"
					}
				},
				{
					prop: "pic",
					label: "封面图片",
					component: {
						name: "cl-upload",
						props: {
							text: "选择图片"
						}
					}
				}
            ]
        });

        // 表格配置
        const table = reactive<Table>({
            columns: [
                {
					type: "selection",
					width: 60
				},
				{
					label: "ID",
					prop: "id"
				},
                {
					prop: "name",
					label: "名称",
					align: "center",
				},
				{
					prop: "pic",
					label: "封面图片",
				},
				{
					prop: "sortNum",
					label: "排序等级",
					align: "center",
				},
				{
					label: "操作",
					type: "op",
					buttons: [ "edit", "delete"]
				}
            ]
        });

        // crud 加载
        function onLoad({ ctx, app }: CrudLoad) {
            // 绑定 service
            ctx.service(service.original.tags).done();
            app.refresh();
        }

        return {
            refs,
            setRefs,
            upsert,
            table,
            onLoad
        };
    }
});
</script>
